package Client;

import SQL.Connections;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.JOptionPane;

//管理员发布
public class AdminPost extends JFrame{
    private JFrame frame;
    private JPanel j1;
    private JFormattedTextField formattedTextField1;
    private JFormattedTextField formattedTextField2;
    private JFormattedTextField formattedTextField3;
    private JFormattedTextField formattedTextField4;
    private JFormattedTextField formattedTextField5;
    private JFormattedTextField formattedTextField6;
    private JFormattedTextField formattedTextField7;
    private JFormattedTextField formattedTextField8;
    private JButton 确定Button;
    private JButton 取消Button;
    private JOptionPane jOptionPane;

    private String trainNo;
    private int rsClasses;
    private Statement stm;

    public AdminPost() {
        确定Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                trainNo = formattedTextField1.getText();
                String flightNo = formattedTextField2.getText();
                String startPoint = formattedTextField3.getText();
                String destination = formattedTextField4.getText();
                String startTime = formattedTextField5.getText();
                String firstClassPrice = formattedTextField6.getText();
                String secondClassPrice = formattedTextField7.getText();
                String distance = formattedTextField7.getText();
                post(trainNo,flightNo,startPoint,destination,startTime,firstClassPrice,secondClassPrice,distance);
                AdminPage.initialize();
                frame.dispose();
            }
        });
        取消Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AdminPage.initialize();
                frame.dispose();
            }
        });
    }

    public static void main(String[] args) {

    }
    
    public static void initialize() {
        AdminPost adminPost = new AdminPost();
        adminPost.frame = adminPost;
        adminPost.setContentPane(adminPost.j1);
        adminPost.setLocationRelativeTo(null);
        adminPost.pack();
        adminPost.setVisible(true);
    }
    
    private void post(String trainNO,String flightNO,String startPoint,String destination,String startTime,String firstClassPrice,String secondClassPrice,String distance) {
        try {

            Connections connections = new Connections();
            Connection con = connections.getConnection();
            stm = con.createStatement();
            rsClasses = stm.executeUpdate("insert into Classes values ('" + flightNO + "','" + trainNO + "','" + startPoint + "','" + destination + "','" + startTime + "','" + 200 + "','" + distance + "','"+200+"')");
            int rsTrain = stm.executeUpdate("insert into Train values ('" + trainNO + "','" + 120 + "','" + firstClassPrice + "','" + secondClassPrice + "')");
            if (rsTrain == 1 && rsClasses == 1) {
                jOptionPane.showMessageDialog(null, "发布成功", "系统提示", JOptionPane.INFORMATION_MESSAGE);
            }
        }catch (SQLIntegrityConstraintViolationException sqlEX){
            try {
                String sql = "update Train set TrainFirstPrice = '"+firstClassPrice+"',TrainSecondPrice = '"+secondClassPrice+"' where TrainNumber = '"+trainNo+"'";
                int rsTrain = stm.executeUpdate(sql);
                if (rsTrain == 1 && rsClasses == 1) {
                    jOptionPane.showMessageDialog(null, "发布成功", "系统提示", JOptionPane.INFORMATION_MESSAGE);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }catch (Exception e) {
            jOptionPane.showMessageDialog(null,"发布失败","系统提示",JOptionPane.INFORMATION_MESSAGE);
        }
    }
}
